import moment from "moment";
import "moment/dist/locale/zh-cn";
import { sign } from "@/apis/member.ts";
export default {
  rpx2px: (v: any) => {
    return (uni.getSystemInfoSync().windowWidth * v) / 750;
  },
  goBack: () => {
    uni.navigateBack({
      fail: () => {
        uni.switchTab({
          url: "/pages/home/index",
        });
      },
    });
  },
  /*
路由跳转
*/
  goHerf: (url: any, mode: any = "", params: any = "") => {
    switch (mode) {
      case "tabbar":
        uni.switchTab({
          url,
        });
        break;
      case "redirect":
        uni.redirectTo({
          url,
        });
        break;
      case "reLaunch":
        uni.reLaunch({
          url,
        });
        break;
      default:
        uni.navigateTo({
          url: url,
        });
    }
  },
  // 处理入参
  handleQuery: (query: any) => {
    if (query.scene) {
      let data: any = {};
      let sceneParams: any = decodeURIComponent(decodeURI(query.scene));
      sceneParams = sceneParams.split("&");
      if (sceneParams.length) {
        sceneParams.forEach((ele: any) => {
          let params = ele.split("=");
          data[params[0]] = params[1];
        });
      }
      return data;
    } else {
      return query;
    }
  },
  // 获取富文本
  handleHtml: (content: any) => {
    let textHtml = "";
    let contentArr: any = [];
    let texts = content.split("</p>");

    texts.map((eleP: any) => {
      if (eleP.indexOf("<br") != -1) {
        contentArr.push({
          type: "symbol",
          content: "",
        });
      } else if (eleP.indexOf("<img") != -1) {
        let imgs = eleP.split("<img");
        console.log(imgs);

        imgs.map((eleImg: any) => {
          if (eleImg.indexOf('src="') != -1) {
            let contents = eleImg.split('src="');
            let content = contents[1].split('"');
            let src = content[0];
            contentArr.push({
              type: "img",
              content: src,
            });
          }
        });
      } else {
        let content;
        let center = 0,
          fontSize = 0,
          color = 0;
        if (eleP.indexOf("text-align") != -1) {
          center = 1;
        }
        if (eleP.indexOf("text-align") != -1) {
          center = 1;
        }
        if (eleP.indexOf("color: rgb(0, 0, 255);") != -1) {
          color = 1;
        }
        if (eleP.indexOf("font-size: 24px;") != -1) {
          fontSize = 1;
        }
        if (eleP.indexOf("span") != -1) {
          let span = eleP.split("</span>");
          span = span[0].split(">");
          content = span[span.length - 1];
        } else {
          let contents = eleP.split(">");
          content = contents[contents.length - 1];
        }

        if (content.indexOf("&nbsp;") != -1) {
          content = content.replace(/&nbsp;/g, " ");
        }
        if (content) {
          contentArr.push({
            type: "text",
            content: content,
            center,
            fontSize,
            color,
          });
          textHtml += `${content}`;
        }
      }
    });
    return contentArr;
  },
  // 获取富文本文本集
  handleHtmlTxt: (content: any, format: any = false) => {
    let textHtml = "";
    let contentArr: any = [];
    let texts = content.split("</p>");

    texts.map((eleP: any) => {
      if (eleP.indexOf("<br") == -1 && eleP.indexOf("<img") == -1) {
        let content;
        let center = 0,
          fontSize = 0,
          color = 0;
        if (eleP.indexOf("text-align") != -1) {
          center = 1;
        }
        if (eleP.indexOf("text-align") != -1) {
          center = 1;
        }
        if (eleP.indexOf("color: rgb(0, 0, 255);") != -1) {
          color = 1;
        }
        if (eleP.indexOf("font-size: 24px;") != -1) {
          fontSize = 1;
        }
        if (eleP.indexOf("span") != -1) {
          let span = eleP.split("</span>");
          span = span[0].split(">");
          content = span[span.length - 1];
        } else {
          let contents = eleP.split(">");
          content = contents[contents.length - 1];
        }

        if (content.indexOf("&nbsp;") != -1) {
          content = content.replace(/&nbsp;/g, " ");
        }
        if (content) {
          contentArr.push({
            type: "text",
            content: content,
            center,
            fontSize,
            color,
          });
          textHtml += `${content}`;
        }
      }
    });
    return format ? contentArr : textHtml;
  },
  // 获取富文本图片集
  getRichTextImages: (content: any) => {
    let images: any = [];
    let htmls = content.split("</p>");
    htmls.map((ele: any) => {
      if (ele.indexOf("<img") != -1) {
        let contents = ele.split('src="');
        let content = contents[1].split('"');
        let src = content[0];
        images.push({
          type: "img",
          src,
        });
      }
    });
    return images;
  },

  formetaTime: (time: any, format: any = "YYYY年MM月DD日") => {
    return moment(time).format(format);
  },
  formeta: (time: any) => {
    let times = moment(time).startOf("second").fromNow().split(" ");
    if (times[0]) {
      if (+times[0] > 2880) {
        return moment(time).format("MM-DD");
      } else if (+times[0] > 1440) {
        return moment().subtract(time, "days").calendar();
      } else if (+times[0] > 60) {
        return moment(time).format("a hh:mm");
      } else {
        return moment(time).startOf("second").fromNow();
      }
    }
  },
  getUserinfo: async () => {
    // #ifdef MP-WEIXIN
    return new Promise((resolve: any) => {
      uni.login({
        success(res: any) {
          sign({ code: res.code }).then((signRes: any) => {
            resolve();
            if (signRes.code == 0 || signRes.code == 200) {
              uni.setStorageSync("token", signRes.data.token);
              uni.setStorageSync("userinfo", signRes.data);
            }
          });
        },
      });
    });
    // #endif
  },
  updateUserinfo: () => {},
};
